package org.budo.cmbc.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.budo.support.lang.util.BufferedReaderUtil;
import org.budo.support.lang.util.IoUtil;
import org.budo.support.lang.util.MapUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.spring.io.util.ResourceUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
public class BankCodeUtil {
    private static Map<String, String> BANK_CODE_MAP = null;

    public static String bankCode(String bankName, String subBankName) {
        initBankCodeMap();

        if (null == subBankName || null == BANK_CODE_MAP) {
            log.error("#25 bankCode, subBankName=" + subBankName + ", bankName=" + bankName + ", BANK_CODE_MAP=" + BANK_CODE_MAP);
            return null;
        }

        String subBankCode = BANK_CODE_MAP.get(subBankName.trim());
        if (null != subBankCode && !subBankCode.trim().isEmpty()) {
            return subBankCode;
        }

        if ("建设银行".equals(bankName.trim()) || "中国建设银行".equals(bankName.trim())) {
            return "105100000017";
        }

        if ("工商银行".equals(bankName.trim()) || "中国工商银行".equals(bankName.trim())) {
            return "102100099996";
        }

        if ("农业银行".equals(bankName.trim()) || "中国农业银行".equals(bankName.trim())) {
            return "103100000026";
        }

        if ("招商银行".equals(bankName.trim())) {
            return "308584000013";
        }

        if ("中国银行".equals(bankName.trim())) {
            return "104100000004";
        }

        if ("交通银行".equals(bankName.trim())) {
            return "301290000007";
        }

        if ("民生银行".equals(bankName.trim())) {
            return "305100000013";
        }

        log.warn("#64 bankCode, bankCode=" + subBankCode + ", bankName=" + bankName + ", subBankName=" + subBankName //
                + ", size=" + MapUtil.size(BANK_CODE_MAP) + ", firstEntry=" + MapUtil.firstEntry(BANK_CODE_MAP));
        return subBankCode;
    }

    private static void initBankCodeMap() {
        if (null != BANK_CODE_MAP) {
            return;
        }

        synchronized (BankCodeUtil.class) {
            if (null != BANK_CODE_MAP) {
                return;
            }

            log.info("#42 init BANK_CODE_MAP");

            Map<String, String> map = new HashMap<String, String>();

            InputStream inputStream = ResourceUtil.classPathResourceInputStream("大小额联行号信息.txt");
            BufferedReader bufferedReader = new BufferedReader(IoUtil.inputStreamReader(inputStream, "GBK"));
            String line;
            while ((line = BufferedReaderUtil.readLine(bufferedReader)) != null) {
                String[] split = StringUtil.split(line, "\\|");
                String _bankCode = split[0].trim();
                String _bankName = split[12].trim();

                map.put(_bankName, _bankCode);
            }

            BANK_CODE_MAP = map;

            IoUtil.closeQuietly(inputStream);

            log.info("#61 after init BANK_CODE_MAP, size=" + MapUtil.size(BANK_CODE_MAP) + ", firstEntry=" + MapUtil.firstEntry(BANK_CODE_MAP));
        }
    }
}
